<?php

namespace DebugComponent\Renderer;

/**
 * PHP template renderer
 */
class PHPRenderer implements \DebugComponent\ITemplateRenderer {

    /**
     * Class constructor
     *
     * @return PHPRenderer instance
     */
    public function  __construct() {
        $this->template = null;
    }

    /**
     * Retrieve template
     *
     * @return string
     * @see \DebugComponent\ITemplateRenderer
     */
    public function getTemplate() {
        return $this->template;
    }

    /**
     * Set template
     *
     * @param string $template
     * @see \DebugComponent\ITemplateRenderer
     */
    public function setTemplate($template) {
        if (!\file_exists($template)) {
            throw new \RuntimeException('Template "' . $template . '" not found');
        }
        $this->template = $template;
    }

    /**
     * Render template with apropriate data
     *
     * @param \DebugComponent\ParameterHolder $data
     * @return string
     * @see \DebugComponent\IRenderer
     */
    public function render(\DebugComponent\ParameterHolder $data) {
       if ($this->template === null) {
           throw new \RuntimeException('Missing template name');
       }
       \ob_start();
       require $this->template;
       return \ob_get_clean();
    }

    /**
     * @var string template name
     */
    private $template;
}